package com.example.depcontrol.aop;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Aspect//AOP类
@Order(1)
public class TimeAspect {

    @Pointcut("@annotation(com.example.depcontrol.aop.MyLog)")//表示匹配有@MyLog注解的方法
    public void pr(){}

    @Around("pr()")
    public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
        //1.记录开始时间
            long begin = System.currentTimeMillis();
        //2.调用原始方法
            Object result = joinPoint.proceed();
        //3.记录结束时间，计算耗时
            long end = System.currentTimeMillis();
            log.info(joinPoint.getSignature() + "耗时为：{}ms", end-begin);

            return result;
    }
}
